import type { Router } from 'vue-router'
import { isNumber } from '@/utils'
import { menuHandler, generIdMap, generPathMap } from './utils'

/*
 第一层作为大菜单
 第二层作为顶部菜单
*/
export interface NavMenuItem {
  id: number
  path?: string
  title: string
  color?: string
  icon?: string
  children?: NavMenuItem[]
  pid?: number
  level: number
}

// 特殊路由
export const NAV_BAR_MENU_PATH = '/nav-menu'
export const SECOND_MENU_PATH = '/system-home/second-menu'

// 主页
export const homeNavMenu: NavMenuItem[] = menuHandler([
  {
    path: '/yearCheck',
    title: '年检年报',
    icon: '1'
  },
  {
    path: '/spotCheck',
    title: '检查抽查',
    icon: '2'
  },
  {
    path: '/riskDisposal',
    title: '风险处置',
    icon: '3'
  },
  {
    path: '/evaluate',
    title: '综合评价',
    icon: '4'
  }
])
// 愉快办主页
export const ykbHomeNavMenu= menuHandler([
  {
    path: '/ykb/index',
    title: '年检年报',
    icon: '1'
  },
  {
    path: 'ykb/activity',
    pathPhone: 'phone/activity/list',
    title: '活动管理',
    icon: '2'
  }
])

// 主页-bar
export const barNavMenu: NavMenuItem[] = menuHandler([
  {
    title: '系统管理',
    path: '/systemSetting',
    icon: 'setting',
    // children: [
    //   {
    //     path: '/systemSetting/organization',
    //     title: '组织管理',
    //     icon: 'domainManagement'
    //   },
    //   {
    //     path: '/systemSetting/personnelManage',
    //     title: '用户管理',
    //     icon: 'userManagement'
    //   },
    //   {
    //     path: '/systemSetting/rolePermission',
    //     title: '角色权限',
    //     icon: 'roleManagement'
    //   },
    //   {
    //     path: '/systemSetting/logManagement',
    //     title: '日志管理',
    //     icon: 'labelManagement'
    //   },
    //   {
    //     path: '/systemSetting/paramsManagement',
    //     title: '参数管理',
    //     icon: 'lineManagement'
    //   }
    // ]
  },
])

const allNavMenu = [
  ...homeNavMenu,
  ...barNavMenu,
  // SECOND_MENU_PATH children 顶部菜单
  ...barNavMenu
    .filter((item) => item.path === SECOND_MENU_PATH)
    .reduce((arr: NavMenuItem[], item) => arr.concat(item.children || []), [])
]

const idMap = generIdMap(allNavMenu)
const pathMap = generPathMap(allNavMenu)

export function getMenu(x: number | string) {
  if (isNumber(x)) {
    return idMap.get(x)
  }

  if (pathMap.get(x)) {
    return pathMap.get(x)
  }
  let _path = ''
  const paths = x
    .split('/')
    .filter(Boolean)
    .map((v) => {
      _path = _path + '/' + v
      return _path
    })
    .reverse()

  for (let i = 0; i < paths.length; i++) {
    const menu = pathMap.get(paths[i])
    if (menu) return menu
  }
}

export function getTopLevelMenu(x: number | string): NavMenuItem | undefined {
  if (isNumber(x)) {
    let menu = getMenu(x)

    while (menu && menu.pid) {
      menu = getMenu(menu.pid)
      console.log(menu, 'menu')
    }

    return menu
  }

  const id = getMenu(x)?.id
  if (id) {
    return getTopLevelMenu(id)
  }
}

export const navMenuGoPath = (router: Router, item: NavMenuItem) => {
  console.log('item', item)

  console.log('routes', router.getRoutes())

  if (!item.path) return

  if ([NAV_BAR_MENU_PATH, SECOND_MENU_PATH].includes(item.path)) {
    return router.push(`${item.path}?id=${item.id}`)
  }

  router.push(item.path)
}
